package ModelLayer;

import java.util.ArrayList;

/**
 * This class is a container class for ToolDescription.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ToolContainer
{
    private ArrayList<ToolDescription>  toolDescriptionList;
    private static ToolContainer        instance;
    /**
     * Private constructor for objects of class ToolContainer
     */
    private ToolContainer()
    {
        toolDescriptionList = new ArrayList<ToolDescription>();
    }
    
    /**
     * This method returns an instance of this class, in accordance with singelton implementation.
     */
    public static ToolContainer getInstance()
    {
        if(instance == (null))
        {
            instance = new ToolContainer();
        }
        return instance;
    }
    
    /*
     * Private method returns the index of a discripton by id.
     */
    private int getDescriptionIndex(long id)
    {
        int index = -1;
        int i = 0;
        boolean found = false;
        while(!found && i<toolDescriptionList.size())
        {
            if(toolDescriptionList.get(i).getDescriptionID() == id)
            {
                index = i;
                found = true;
            }
            i++;
        }
        return index;
    }
    
    /**
     * This method adds a tool description.
     * 
     * @param   toolDescription A tool description.
     */
    public void addToolDescription(ToolDescription toolDescription)
    {
        toolDescriptionList.add(toolDescription);
    }
    
    /**
     * This method updates the attributes of a tool description.
     * 
     * @param   id          The id of the tool description.
     * @param   description The new description.
     * @param   price       The new price.
     */
    public void editToolDescription(long id, String description, double price)
    {
        toolDescriptionList.get(getDescriptionIndex(id)).setDescription(description);
        toolDescriptionList.get(getDescriptionIndex(id)).setPrice(price);
    }
    
    /**
     * This method returns a tool description.
     * 
     * @param   id  The id of the tool description.
     * 
     * @return  The tool Descrriptionn.
     */
    public ToolDescription getToolDescription(long id)
    {
        return toolDescriptionList.get(getDescriptionIndex(id));
    }
    
    /**
     * This method adds a tool to a tool description.
     * 
     * @param   descriptionID   The id of the tool description.
     * @param   tool            The Tool.
     */
    public void addTool(long descriptionID, Tool tool)
    {
        try
        {
            toolDescriptionList.get(getDescriptionIndex(descriptionID)).addTool(tool);
        }
        catch(Exception e){}
    }
    
    /**
     * This method returns a Tool.
     * 
     * @param   descriptionID   The id of the tool description. As a long.
     * @param   serialNumber    The serialNumber of the tool. As a long.
     * 
     * @return  The tool. As a Tool.
     */
    public Tool getTool(long descriptionID, long serialNumber)
    {
        Tool tool = null;
        try
        {
            toolDescriptionList.get(getDescriptionIndex(descriptionID)).getTool(serialNumber);
        }
        catch(Exception e){/* Do something*/}
        return tool;
    }
    
    /**
     * This method deletes a tool from a tool description.
     * 
     * @param   descriptionID   The id of the tool description.
     * @param   serialNumber    The serial number of the tool.
     */
    public void deleteTool(long descriptionID, long serialNumber)
    {
        toolDescriptionList.get(getDescriptionIndex(descriptionID)).deleteTool(serialNumber);
    }
    
    /**
     * This method returns a list of tool descriptions
     * 
     * @return  a list of tool descriptions.
     */
    public ArrayList<ToolDescription> getToolDescriptionList()
    {
        return toolDescriptionList;
    }
}
